package edu.metrostate.capstonediner;

import java.util.ArrayList;

public class TextBuffer extends SpriteBuffer{
	// Contains a list of all of the text that need to be displayed
	private ArrayList<DrawableText> textList = new ArrayList<DrawableText>();
	// Used to store the texture for sprites
	FontTexture textTexture;
	
	public void setTexture(FontTexture texture) {
		this.texture = texture;
		textTexture = texture;
	}
	
	public void add(DrawableText text) {
		synchronized(listLock) {
			textList.add(text);
			//size = size + text.getText().length();
			
			// Checks to see if buffers need to be increased in size
			if (size > maxSprites) {
				createBuffers(size * 2);
			}
		}
		
		update();
	}
	
	public void remove(DrawableText text) {
		synchronized(listLock) {
			textList.remove(text);
			//size = textList.size();
		}
		
		update();
	}
	
	public void update() {
		float x;
		float y;
		float width;
		float height;
		float fontRatio;
		
		float textureX;
		float textureY;
		float textureWidth;
		float textureHeight;
		
		DrawableText text;
		char letter;
		int checkSize = 0;
		
		// Used to store the current index for the vertices
		int verticesIndex = 0;
		// Used to store the current index for the indices
		short indicesIndex = 0;
		
		synchronized(listLock) {
			if (texture == null) {
				return;
			}
			
			for (int i = 0; i < textList.size(); i++) {
				checkSize += textList.get(i).getText().length();
			}
			
			// Checks to see if buffers need to be increased in size
			if (checkSize > maxSprites) {
				createBuffers(checkSize * 2);
			}
			
			checkSize = 0;
			
			//get each part's location
			for (int i = 0; i < textList.size(); i++) {
				text = textList.get(i);
				
				x = text.getX();
				y = text.getY();
				
				height = text.getFontSize();
				textureHeight = textTexture.getHeight();
				fontRatio = height / textureHeight;
				
				
				for (int position = 0; position < text.getText().length(); position++) {
					letter = text.getText().charAt(position);
					
					textureX = textTexture.getX(letter);
					textureY = textTexture.getY(letter, text.getFont());
					textureWidth = textTexture.getWidth(letter, text.getFont());
					width = fontRatio * textureWidth;
					
					verticesList[verticesIndex++] = x;
					verticesList[verticesIndex++] = y;
					verticesList[verticesIndex++] = textureX;
					verticesList[verticesIndex++] = textureY;
					verticesList[verticesIndex++] = x;
					verticesList[verticesIndex++] = y + height;
					verticesList[verticesIndex++] = textureX;
					verticesList[verticesIndex++] = textureY + textureHeight;
					verticesList[verticesIndex++] = x + width;
					verticesList[verticesIndex++] = y;
					verticesList[verticesIndex++] = textureX + textureWidth;
					verticesList[verticesIndex++] = textureY;
					verticesList[verticesIndex++] = x + width;
					verticesList[verticesIndex++] = y + height;
					verticesList[verticesIndex++] = textureX + textureWidth;
					verticesList[verticesIndex++] = textureY + textureHeight;
					
					indicesList[indicesIndex++] = (short) (VERTICES_PER_SPRITE * checkSize + 0);
					indicesList[indicesIndex++] = (short) (VERTICES_PER_SPRITE * checkSize + 1);
					indicesList[indicesIndex++] = (short) (VERTICES_PER_SPRITE * checkSize + 2);
					indicesList[indicesIndex++] = (short) (VERTICES_PER_SPRITE * checkSize + 1);
					indicesList[indicesIndex++] = (short) (VERTICES_PER_SPRITE * checkSize + 2);
					indicesList[indicesIndex++] = (short) (VERTICES_PER_SPRITE * checkSize + 3);
					
					checkSize++;
					
					x += width;
				}
			}
			size = checkSize;
		}
	}
}
